//
//  HostConfig.m
//  CoinEgg_App
//
//  Created by tjl on 2018/7/9.
//  Copyright © 2018年 tjl. All rights reserved.
//

#import "HostConfig.h"
#import "config.h"
#import "NetworkHelper.h"

@implementation HostConfig

- (instancetype)sharedHostAddress {
    
    if (self == [super init]) {
        
        [self timePing];
        
    }
    return self;
    
}

- (void)timePing {
    
    if ([HOST containsString:@"app.cointest.com"]) {
        return;
    }
    
    [self moreDuoYuming];
    return;
    
}

- (NSArray *)theUrlArray {
    
    return @[@"https://app.coinegg.im", @"https://app1.coinegg.im", @"https://app2.coinegg.im", @"https://app3.coinegg.im"];
    
}

// 开线程,因为任务是异步的
// 无序,多个任务要并发执行必须要满足,任务是异步的.队列是并发的
- (void)moreDuoYuming

{
    
    if (![HOST containsString:@"https:"]) {
        return;
    }
    
    NSArray *ura = [self theUrlArray];
    //利用GCD并行多个线程并且等待所有线程结束之后再执行其它任务
    dispatch_group_t group = dispatch_group_create();
    for (int i = 0; i < ura.count; i++) {

        dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
            // 并行执行的线程一
            [self requestTimeWithUrl:ura[i]];
            NSLog(@"%d %@",i,[NSThread currentThread]);
        });
    
    }
    
}

//返回host地址
- (NSString *)hostAddressWithUrl:(NSString *)hota {
    
    NSString *ssss = [NSString stringWithFormat:@"%@/download.html.bk", hota];
    
    return ssss;
    
}

//请求多域名
- (void)requestTimeWithUrl:(NSString *)url {
    
    NSString *time = [self getNowTimeTimestamp];
    NSLog(@"requestTimeWithUrl:-%@:%@", url, time);
    [NetworkHelper downloadWithURL:[self hostAddressWithUrl:url] fileDir:url progress:^(NSProgress *progress) {
        
    } success:^(NSString *filePath) {
        if (filePath) {
            [self requestTimeWithUrlSuccessful:url];
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)requestTimeWithUrlSuccessful:(NSString *)url {
    WS(weakSelf, self);
    dispatch_async(dispatch_get_main_queue(), ^{
        if (!weakSelf.isSetHost) {
            weakSelf.isSetHost = YES;
            [[NSUserDefaults standardUserDefaults] setObject:url forKey:@"hostAddress_String"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    });
    
}

- (NSString *)getNowTimeTimestamp {
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSTimeInterval a=[dat timeIntervalSince1970];
    
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    
    return timeString;
    
}


+ (NSString *)hostAddress {
    
//    return @[@"https://app.coinegg.im", @"https://app1.coinegg.im", @"https://app2.coinegg.im", @"https://app3.coinegg.im"][arc4random()%4];
    
    NSUserDefaults *stan = [NSUserDefaults standardUserDefaults];
    NSString *url_str = [stan objectForKey:@"hostAddress_String"];
    
    //url判断
    if (url_str && url_str.length && ![url_str containsString:@"null"]) {
        return url_str;
    }
    
    return @"https://app.coinegg.im";
    
}

@end
